<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="../css/common.css"/>
    <link rel="stylesheet" href="../css/main.css"/>
    <link rel="stylesheet" href="../css/jquery-ui-timepicker-addon.css"/>
    <link rel="stylesheet" href="../css/jquery-ui.css"/>
    <link href="../css/bootstrap.css" rel="stylesheet" type="text/css" />
    <link href="../css/city-picker.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" href="../css/jquery-impromptu.min.css" type="text/css"/>
    <title>Document</title>
    <style>
    </style>
</head>
<body>
    <div class="box">
        <div class="box_border">
            <div class="box_top"><b class="pl15">部门信息</b></div>
            <div class=" pt10 pb10" >
                <div class="box span9 oh" style="padding-left: 35%; margin-right: 0px;">
                <table class="form_table" border="0" cellpadding="0" cellspacing="0"  >
                    <tr>
                        <td><b>部门名称:</b><input type="text" id="section_name" name="section_name" class="input-text lh30" size="40"></td>
                        <td><input type="button" value="保存" class="ext_btn ext_btn_submit" onclick="editSectionData()"></td>
                        <td><input type="button" value="删除部门"  class="ext_btn ext_btn_submit" onclick="deleteSection()"></td>
                    </tr>
                </table>
                </div>
            </div>
        </div>
    </div>
    <div class="box">
        <div class="box_border" style="border-bottom: none;">
            <div class="box_top"><b class="pl15">部门人员信息</b>
            </div>
            <div style="clear: both;"></div>
            <div class="mt10">
                <div id="table" class="mt10">
                    <div class="box span9 oh" style="margin-left: auto; margin-right: auto">
                        <table class="form_table" border="0" cellpadding="0" cellspacing="0">
                            <tr>
                                <td><input type="text"  class="input-text lh30" size="40" id="searchText"></td>
                                <td><a class="ext_btn"  onclick="searchSectionUser()"><span class="browse"></span>搜索员工</a></td>
                                <td><a class="ext_btn"  onclick="addUserToSection()"><span class="add"></span>添加员工</a></td>
                            </tr>
                        </table>
                    </div>
                    <div class="box span9 oh" style="margin-left: auto; margin-right: auto">
                        <table id="approval_info1" width="100%" border="0" cellpadding="0" cellspacing="0" class="list_table" style="table-layout:fixed; font-size:1.2rem">
                            <tr>
                                <th class="td_center" style="width:5%;text-align: center;"><input type="checkbox" name="userCheckAll" onclick="checkAll()" ></th>
                                <th class="td_center" style="width:5%">序号</th>
                                <th class="td_center" >账号</th>
                                <th class="td_center" >姓名</th>
                                <th class="td_center" >职位</th>
                                <th class="td_center" >可选操作</th>
                            </tr>
                            <tbody id="approval_info">

                            </tbody>
                        </table>
                        <div class="page mt10 fl">
                            <a class="ext_btn del mt10" onclick="batchDelete()" ><span class="del" ></span>批量删除</a>
                            <!--<a class="ext_btn del mt10" ><span class="del" ></span>删除全部</a>-->
                        </div>
                        <div class="page  fr" style="margin-top: -10px;">
                            <div class="pagination">
                                <ul id="ul">
                                    <li class="first-child " id="li1"><a onclick="firstLastPage(0)">首页</a></li>
                                    <li class="" id="li2"><a onclick="upDownPage(-1)">上一页</a></li>
                                    <li class="" id="li3"><a onclick="upDownPage(1)">下一页</a></li>
                                    <li class="last-child " id="li4"><a onclick="firstLastPage(1)">末页</a></li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
<div id="info" style="display: none;text-align: center;" ></div>
</body>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script type="text/javascript" src="../js/jquery-ui.js"></script>
<script type="text/javascript" src="../js/common.js"></script>
<script type="text/javascript" src="../js/config.js"></script>
<script type="text/javascript" src="../js/config.js"></script>
<script type="text/javascript">

    //获取基本部门信息
    function getSectionName(){
        var phpUrl=url+'admin/Section/SectionGetSectionData.php';
        $.ajax({
            url:phpUrl,
            type:"POST",
            data:{model:'getSectionData',section_ID:sectionId},
            success: function (sectionData) {
//                alert(sectionData);
                sectionData=JSON.parse(sectionData);
                if(sectionData['state']=-1) {
                   var sectionData=sectionData['data'][0];
//                    alert(sectionData['section_name']);
                    $("#section_name").val(sectionData['section_name']);
//                    sectionId=sectionData['section_ID'];
                }
            }
        })
    }

    //编辑部门
    function editSectionData(){
        var phpUrl=url+'admin/Section/SectionUpdateSectionData.php';
        var sectionName= $("#section_name").val();
//        alert(sectionName);
        if(sectionName==''){dialogInfo("部门名称不能为空！");setTimeout('closeDialogInfo()',1000);}
        else {
            $.ajax({
                url: phpUrl,
                type: "POST",
                data: {model: 'updateSectionData', section_ID: sectionId, section_name: $("#section_name").val()},
                success: function (state) {
                    state = JSON.parse(state);
                    if (state['state'] != -1)
                        document.getElementById('info').innerHTML = "保存成功！";
                    $("#info").dialog({
                        resizable: false,
                        title: "信息",
                        modal: true,
                        buttons: {
                            '确定': function () {
                                $("#info").dialog('close');
                                getSectionName();
                            }
                        }
                    });
                }
            });
        }
    }
    //删除部门
    function deleteSection(){
        var phpUrl=url+'admin/Section/SectionDeleteSectionData.php';
        document.getElementById('info').innerHTML="<p style='color: red;'>你确定要删除部门吗？<br/>删除部门同时会删除部门任职的信息！</p>";
        $("#info").dialog({
            resizable:false,
            title: "信息",
            modal: true,
            buttons:{
                '取消': function () {
                    $("#info").dialog('close');
                },
                '确定':function(){
                    $("#info").dialog('close');
//                    alert(sectionId);
                    $.ajax({
                        url:phpUrl,
                        type:"POST",
                        data:{model:"deleteSection",section_ID:sectionId},
                        success: function (state) {
//                            alert(state);
                            state=JSON.parse(state);
                            if(state['state']==1){
                                document.getElementById('info').innerHTML="删除成功！";
                                $("#info").dialog({
                                    resizable: false,
                                    title: "信息",
                                    modal: true,
                                    buttons: {
                                        '确定': function () {
                                            $("#info").dialog('close');
                                            window.location="sectionmanager.html";
                                        }
                                    }
                                });
                            }else{
                                document.getElementById('info').innerHTML="删除失败！";
                                $("#info").dialog({
                                    resizable: false,
                                    title: "信息",
                                    modal: true,
                                    buttons: {
                                        '确定': function () {
                                            $("#info").dialog('close');
//                                            window.localStorage = 'sectionmanager.html';
                                        }
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });
    }
    //加载表数据
    function createTable(n,m){
        var phpUrl=url+'admin/Section/SectionGetSectionUserData.php';
        $.ajax({
            url:phpUrl,
            type: "POST",
            data:{model:'getSectionUserDate',section_ID:sectionId,n:n,m:m},
            success:function(allSectionData){
//                alert(allSectionData);
                allSectionData=JSON.parse(allSectionData);
                $("#approval_info").empty();
                if(allSectionData['state']!=-1){
                    var sectionData=allSectionData['data'];
                    for(var i=0;i<sectionData.length;i++) {
                        $("#approval_info").append(
                                '<tr><td style="text-align: center;"><input type="checkbox" name="userCheck" onclick="changeCheckAll()" value="'+sectionData[i]['user_ID']+'" ></td> ' +
                                '<td class="td_center" >' + (i+1)+ '</td>'+
                                '<td class="td_center  " >' + sectionData[i]['user_ID'] + '</td>'+
                                '<td class="td_center  " >' + sectionData[i]['user_name'] + '</td>'+
                                '<td class="td_center  " >' + sectionData[i]['section_job'] + '</td>'+
                                '<td class="td_center "><a class="ext_btn delbt"  onclick="deleteSectionUser('+sectionData[i]['user_ID']+')" ><span class="delbt" ></span>删除员工</a></td></tr>'
                        );
                    }
                }else{
                    $("#ul li").addClass("disabled");
                }
            }
        });
    }
    //搜索员工
    function searchSectionUser(){
        var phpUrl=url+'admin/Section/SectionSearchUserData.php';
        var searchText=$("#searchText").val();
//        alert(searchText);
        if(searchText!='')
        $.ajax({
            url:phpUrl,
            type:"POST",
            data:{model:'searchSectionUser',user_name:searchText,section_ID:sectionId},
            success: function (sectionUserData) {
//                alert(sectionId);
//                alert(sectionUserData);
                sectionUserData=JSON.parse(sectionUserData);
//                alert(sectionUserData['state']);
                if(sectionUserData['state']==1){
                    $("#approval_info").empty();
                    var sectionData=sectionUserData['data'];
                    for(var i=0;i<sectionData.length;i++) {
//                        alert(sectionData[i]['user_ID']);
                        $("#approval_info").append(
                                '<tr><td style="text-align: center;"><input type="checkbox" name="userCheck"  onclick="changeCheckAll()" value="'+sectionData[i]['user_ID']+'"></td> ' +
                                '<td class="td_center" >' + (i+1)+ '</td>'+
                                '<td class="td_center  " >' + sectionData[i]['user_ID'] + '</td>'+
                                '<td class="td_center  " >' + sectionData[i]['user_name'] + '</td>'+
                                '<td class="td_center  " >' + sectionData[i]['section_job'] + '</td>'+
                                '<td class="td_center "><a class="ext_btn delbt"  onclick="deleteSectionUser('+sectionData[i]['user_ID']+')" ><span class="delbt" ></span>删除员工</a></td></tr>'
                        );
                    }
                }
            }
        });
        else{
            firstLastPage(0);
        }
    }
    //删除员工
    function deleteSectionUser(user_ID){
//        alert(1);
        var phpUrl=url+'admin/Section/SectionDeleteSectionUserById.php';
        $.ajax({
            url:phpUrl,
            type:"POST",
            data:{model:'deleteSectionUser',user_ID:user_ID,section_ID:sectionId},
            success: function (sectionUserData) {
//                alert(sectionUserData);
                sectionUserData=JSON.parse(sectionUserData);
                if(sectionUserData['state']==1)
                document.getElementById('info').innerHTML="删除成功！";
                else
                document.getElementById('info').innerHTML="删除失败！";
                $("#info").dialog({
                    resizable: false,
                    title: "信息",
                    modal: true,
                    buttons: {
                        '确定': function () {
                            $("#info").dialog("close");
                            getSectionPageCount();
                            if(pageCounts==1) $("#ul li").addClass("disabled");
                             createTable(0,10);
                        }
                    }
                });
            }
        });
    }
    //批量删除
    function batchDelete(){
        $('input[name="userCheckAll"]')[0].checked=false;
        var phpUrl=url+'admin/Section/SectionDeleteSectionUserById.php';
        var check=$('input[name="userCheck"]');
        var flag=false;
        for(var i=0;i<check.length;i++)
            if(check[i].checked){
                flag=1;
                $.ajax({url:phpUrl, type:"POST", data:{model:'deleteSectionUser',user_ID:check[i].value,section_ID:sectionId}});
            }
        if(flag) {
            document.getElementById('info').innerHTML = "删除成功！";
            $("#info").dialog({
                resizable: false,
                title: "信息",
                modal: true,
                buttons: {
                    '确定': function () {
                        $("#info").dialog("close");
                        getSectionPageCount();
                        if(pageCounts==1) $("#ul li").addClass("disabled");
                        createTable(0, 10);
                    }
                }
            });
        }
    }
    //辅助批量删除
    function batchDeleteSectionUser(user_ID){
//        alert(1);
//
        var phpUrl=url+'Getsectionmanager.php';
        $.ajax({
            url:phpUrl,
            type:"POST",
            data:{model:'deleteSectionUser',user_ID:user_ID},
            success: function (sectionUserData) {
                sectionUserData=JSON.parse(sectionUserData);
//                if(sectionUserData['state']==1)
//                    document.getElementById('info').innerHTML="删除成功！";
//                else
//                    document.getElementById('info').innerHTML="删除失败！";
//                $("#info").dialog({
//                    resizable: false,
//                    title: "信息",
//                    modal: true,
//                    buttons: {
//                        '确定': function () {
//                            $("#info").dialog("close");
//                        }
//                    }
//                });
            }
        });
    }
    //全选
    function checkAll(){
        var check=$('input[name="userCheck"]');
        var checkAll=$('input[name="userCheckAll"]');
//        alert(check[0].checked);
        if(checkAll[0].checked){
            for(var i=0;i<check.length;i++)
                check[i].checked=true;
        }else{
            for(var i=0;i<check.length;i++)
                check[i].checked=false;
        }
    }
    //取消全选选项
    function changeCheckAll(){
        $('input[name="userCheckAll"]')[0].checked=false;
    }
    //消息提醒
    function dialogInfo(data){
        document.getElementById('info').innerHTML=data;
        $("#info").dialog({
            resizable:false,
            title: "信息",
            modal: true,
            buttons:{
                '确定':function(){
                    $("#info").dialog('close');
//                    createTable();
                }
            }
        })
    }
    //获取页数
    var pageCounts=0;
    var curPage=0;
    function getSectionPageCount(){
        var phpUrl=url+'admin/Section/SectionGetSectionUserPageCount.php';
        $.ajax({
            url:phpUrl,
            type:"POST",
            async:false,
            data:{model:'getOneSectionPageCount',sectionId:sectionId},
            success: function (data) {
//                alert(data);
                data = JSON.parse(data);
                if (data.state == 1) {
                    pageCounts = Math.ceil(data.data/10);
//                    alert(pageCounts);
                }
            }
        });
    }
    function getSectionDataLimit(n,m){
      createTable(n,m);
    }
    //首尾跳页
    function firstLastPage(e){
        $("#ul").show();
        if(e==0) {
            curPage=0;
            $("#ul li").addClass("disabled");
            $("#li3").removeClass("disabled");
            $("#li4").removeClass("disabled");
        }
        else{
//      alert(pageCounts);
            curPage=pageCounts-1;
            $("#ul li").addClass("disabled");
            $("#li1").removeClass("disabled");
            $("#li2").removeClass("disabled");
        }
        if(pageCounts==1) $("#ul li").addClass("disabled");
        getSectionDataLimit(curPage*10,10);
    }
    //上下翻页
    function upDownPage(e){
        $("#ul").show();
        if(curPage>=0||curpage<=pageCounts-1){
            if(e<0){
                if(curPage>0)  {
                    curPage--;
                    if(curPage>0) $("#ul li").removeClass("disabled");
                    else{
                        $("#ul li").removeClass("disabled");
                        $("#li1").addClass("disabled");
                        $("#li2").addClass("disabled");
                    }
                }
            }else{
                if(curPage<pageCounts-1) {
                    curPage++;
                    if(curPage<pageCounts-1) $("#ul li").removeClass("disabled");
                    else{
                        $("#ul li").removeClass("disabled");
                        $("#li3").addClass("disabled");
                        $("#li4").addClass("disabled");
                    }
                }
            }
            if(pageCounts==1) $("#ul li").addClass("disabled");
            getSectionDataLimit(curPage*10,10);
        }
    }
    //关闭消息info
    function closeDialogInfo(){
        $("#info").dialog('close');
    }
    //增加员工部门
    function addUserToSection() {
        window.location = "sectionmanager_add_user.html";
    }
    var sectionId;
    $(function () {
        var localSectionIdData=JSON.parse(window.localStorage.getItem('editSectionId'));
        sectionId=localSectionIdData['sectionId'];
//        alert(sectionId);
            getSectionPageCount();
        getSectionName();
        firstLastPage(0);
    })
</script>
</html>
  